#include "pch.h"

RobotPartBase::RobotPartBase():m_partType(Part_TypeNone)
							  ,m_partAction(Part_ActionIdle)
							  ,owner_id(0)
{

}

void RobotPartBase::OnUpdateAction(float frameTime)
{
	switch (m_partAction)
	{
	case Part_ActionIdle:
		OnActionIdle(frameTime);
		break;
	case Part_ActionMoveForward:
		OnActionMoveForward(frameTime);
		break;
	case Part_ActionMoveBack:
		OnActionMoveBack(frameTime);
		break;
	default:
		break;
	}
}

void RobotPartBase::SetOwnerId(uint id)
{
	owner_id = id;
}

void RobotPartBase::Reset()
{
	b2Transform t;
	if(m_create_info.reverse)
	{
		t.p.x = -m_create_info.local_position.x + m_create_info.core_position.x;
		t.p.y = m_create_info.local_position.y + m_create_info.core_position.y;
		t.q.Set(-m_create_info.local_rotation); 
	}
	else
	{
		t.p.x = m_create_info.local_position.x + m_create_info.core_position.x;
		t.p.y = m_create_info.local_position.y + m_create_info.core_position.y;
		t.q.Set(m_create_info.local_rotation); 
	}
	SetTransform(t);	

	m_b2BodyPart->SetLinearDamping(0.f);
	m_b2BodyPart->SetLinearVelocity(b2Vec2_zero);
	m_b2BodyPart->SetAngularDamping(0.f);
	m_b2BodyPart->SetAngularVelocity(0.f);
}

void RobotPartBase::UpdateCommand(RobotCommandState c)
{
	m_partAction = ActionManager::Instance()->GetPartAction(c, m_partType);
}

void RobotPartBase::SetTransform(const float2& position, float angle)
{
	m_b2BodyPart->SetTransform(b2Vec2(position.x, position.y), angle);
}

void RobotPartBase::SetTransform(const b2Transform& transform)
{
	m_b2BodyPart->SetTransform(transform.p, transform.q.GetAngle());
}

void RobotPartBase::SetActive(bool flag)
{
	m_b2BodyPart->SetActive(flag);
}

void RobotPartBase::SetPartType(PartType type)
{
	m_partType = type;
}

PartType RobotPartBase::GetPartType()
{
	return m_partType;
}

const b2BodySmart& RobotPartBase::GetBodySmart()
{
	return m_b2BodyPart;
}

RobotPartBody::RobotPartBody():m_Rotation()
{

}

RobotPartBody::~RobotPartBody()
{

}

void RobotPartBody::Create(PartCreateInfo info)
{
	m_create_info = info;
}

void RobotPartBody::CreateBox2D()
{
	BodyInfo b_info;
	b_info.owner_point = this;
	b_info.entity_type = Object_Part;
	b_info.entity_bodytype = b2_dynamicBody;
	
	b2Transform t;
	if(m_create_info.reverse)
	{
		t.p.x = -m_create_info.local_position.x + m_create_info.core_position.x;
		t.p.y = m_create_info.local_position.y + m_create_info.core_position.y;
		t.q.Set(-m_create_info.local_rotation); 
	}
	else
	{
		t.p.x = m_create_info.local_position.x + m_create_info.core_position.x;
		t.p.y = m_create_info.local_position.y + m_create_info.core_position.y;
		t.q.Set(m_create_info.local_rotation); 
	}
		
	Box2DManager::Instance()->CreateBox(b_info, m_b2BodyPart, t, CovertSizeR2B(m_create_info.size), m_create_info.density);
	//m_b2BodyPart->SetActive(true);
}

void RobotPartBody::Initialize()
{
	CreateBox2D();

	m_PartSprite = RenderObjectManager::Instance()->CreateSprite(L"data/texture.dds", float2(0, -1.f), float2(m_create_info.size.x, m_create_info.size.y));
}

void RobotPartBody::SetTransform(float2 position, float angle)
{
	RobotPartBase::SetTransform(position, angle);
}

float2 RobotPartBody::GetPosition()
{
	return m_Poistion;
}

float RobotPartBody::GetRotation()
{
	return m_Rotation;
}

// update
void RobotPartBody::OnUpdate(float frameTime)
{
	b2Vec2 p = m_b2BodyPart->GetPosition();
	m_Poistion = float2(p.x, p.y);
	m_Rotation = m_b2BodyPart->GetAngle();
}

// render
void RobotPartBody::OnPreRender()
{
	if(m_PartSprite)
	{
		m_PartSprite->SetPosition(m_Poistion);
		m_PartSprite->SetRotation(m_Rotation);
		m_PartSprite->PreRender(RenderObjectManager::RenderObjectLayer1);
	}
}

